﻿package mortal.game.view.business
{
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class TradeIcon extends HintBaseIcon
    {
        private var _tradeCache:TradeCache;
        private static var _instance:TradeIcon;

        public function TradeIcon()
        {
            super(ImagesConst.trade_hintIcon);
            if (_instance)
            {
                throw new Error("TradeIcon单例");
            }
            this._tradeCache = Cache.instance.trade;
            return;
        }// end function

        override protected function clickHandler(event:MouseEvent) : void
        {
            var _loc_2:SBusinessInfo = null;
            super.clickHandler(event);
            if (Cache.instance.trade.isTrading)
            {
                MsgManager.showRollTipsMsg(Language.getString(21333));
            }
            else
            {
                this._tradeCache.currentSbusiness = this._tradeCache.requestList.shift() as SBusiness;
                this.setLabel(this._tradeCache.requestList.length);
                if (this._tradeCache.requestList.length == 0)
                {
                    this.hide();
                }
                _loc_2 = this._tradeCache.getTargetSBusinessInfo();
                Alert.show(Language.getStringByParam(21334, NameUtil.getNameByCamp(_loc_2.camp, _loc_2.name)), null, Alert.YES | Alert.NO, null, this.onAcceptTradeRequest);
            }
            return;
        }// end function

        private function onAcceptTradeRequest(param1:int) : void
        {
            if (this._tradeCache.currentSbusiness == null)
            {
                MsgManager.showRollTipsMsg(Language.getString(21335));
                return;
            }
            if (param1 == Alert.YES)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TradeAcceptTrade, this._tradeCache.currentBusinessId));
            }
            else
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TradeRejectTrade, this._tradeCache.currentBusinessId));
            }
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.setLabel(this._tradeCache.requestList.length);
            return;
        }// end function

        public static function get instance() : TradeIcon
        {
            if (!_instance)
            {
                _instance = new TradeIcon;
            }
            return _instance;
        }// end function

    }
}
